<template>
  <div class="drawer-contain">
    <el-drawer title="新建任务"
      :with-header="false"
      :visible="visible"
      size="650px"
      @close="close">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" label-position="top">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <div class="contain-editor">
          <dmavon-editor :content.sync="ruleForm.desc" render="value" />
        </div>
        <el-form-item label="备忘录标签">
          <div class="form-item-tag">
            <d-tag :dynamicTags.sync="dynamicTags" />
          </div>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-select v-model="ruleForm.priority" placeholder="请选择优先级" style="width: 100%;">
            <el-option
              v-for="item in prioritys"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="ruleForm.status" placeholder="请选择任务状态" style="width: 100%;">
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-date-picker style="width: 100%;"
                v-model="ruleForm.begin_time"
                type="date"
                placeholder="开始日期">
              </el-date-picker>
            </el-col>
            <el-col :span="12">
              <el-date-picker style="width: 100%;"
                v-model="ruleForm.end_time"
                type="date"
                placeholder="截止日期">
              </el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div class="drawer-footer">
        <el-button @click="reset('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import moment from 'moment'
import dmavonEditor from '@/components/mavonEditor'
export default {
  components: {
    dmavonEditor
  },
  props: {
    prioritys: {
      type: Array
    },
    status: {
      type: Array
    }
  },
  data () {
    return {
      visible: false,
      ruleForm: {
        name: '',
        desc: '',
        begin_time: '',
        end_time: '',
        status: null,
        priority: null
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ]
      },
      loading: false,
      dynamicTags: []
    }
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields()
      this.ruleForm.desc = ''
      this.visible = false
    },
    open (id) {
      this.visible = true
      this.detaliInfo(id)
    },
    reset(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm.desc = ''
      this.visible = false
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.ruleForm.id,
            pid: this.$route.params.id,
            name: this.ruleForm.name,
            desc: this.ruleForm.desc,
            priority: this.ruleForm.priority,
            status: this.ruleForm.status,
            tag: this.dynamicTags.join(','),
            begin_time: moment(this.ruleForm.begin_time).format('YYYY-MM-DD'),
            end_time: moment(this.ruleForm.begin_time).format('YYYY-MM-DD')
          }
          this.$API({
            name: 'UpdateProjectTask',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200 && res.data.data[0] === 1) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    detaliInfo (id) { // 根据id获取详情
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'DetailProjectTask',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          let data = res.data.data
          this.ruleForm.id = data.id
          this.ruleForm.name = data.name
          this.ruleForm.pid = data.pid
          this.ruleForm.desc = data.desc
          this.ruleForm.priority = data.priority
          this.ruleForm.status = data.status
          this.ruleForm.begin_time = data.begin_time
          this.ruleForm.end_time = data.end_time
          this.dynamicTags = data.tag.split(',')
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-drawer__body {
  overflow-y: auto;
}
.drawer-contain {
  /deep/ .el-drawer__body {
    padding: 0 20px 20px 20px;
  }
  .contain-editor {
    width: 100%;
    height: 300px;
  }
  .form-item-tag {
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    padding: 0 15px;
  }
  .drawer-footer {
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>